﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.ComponentModel;

namespace QuanLyRapChieuPhim.DAO
{
    class PhimDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<PhimDTO> GetList()
        {

            DataTable dt = new DataTable();
            BindingList<PhimDTO> ds = new BindingList<PhimDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Phim", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    PhimDTO Phim = new PhimDTO();
                    Phim.MaPhim = int.Parse(row["maPhim"].ToString());
                    Phim.TenPhim = row["tenPhim"].ToString();
                    Phim.NamSanXuat = int.Parse(row["namSanXuat"].ToString());
                    Phim.HangSanXuat = row["hangSanXuat"].ToString();
                    Phim.GioiThieuPhim = row["gioiThieuBoPhim"].ToString();
                    Phim.Trailer = row["trailer"].ToString();
                    Phim.Poster = row["poster"].ToString();
                    Phim.ThoiLuongPhim = int.Parse(row["thoiLuongPhim"].ToString());
                    Phim.MaDinhDangPhim = int.Parse(row["maDinhDangPhim"].ToString());
                    Phim.MaTrangThaiPhim = int.Parse(row["maTrangThaiPhim"].ToString());
                    ds.Add(Phim);
                }
                return ds;
            }
            return null;
        }

        public BindingList<PhimDTO> GetList_QL()
        {

            DataTable dt = new DataTable();
            BindingList<PhimDTO> ds = new BindingList<PhimDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Phim_QL", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    PhimDTO Phim = new PhimDTO();
                    Phim.GioiThieuPhim = row["gioiThieuBoPhim"].ToString();
                    Phim.HangSanXuat = row["hangSanXuat"].ToString();
                    Phim.MaDinhDangPhim = int.Parse(row["maDinhDangPhim"].ToString());
                    Phim.MaPhim = int.Parse(row["maPhim"].ToString());
                    Phim.MaTrangThaiPhim = int.Parse(row["maTrangThaiPhim"].ToString());
                    Phim.NamSanXuat = int.Parse(row["namSanXuat"].ToString());
                    Phim.Poster = row["poster"].ToString();
                    Phim.TenPhim = row["tenPhim"].ToString();
                    Phim.ThoiLuongPhim = int.Parse(row["thoiLuongPhim"].ToString());
                    Phim.Trailer = row["trailer"].ToString();
                    Phim.TenDinhDangPhim = row["tenLoaiKyThuat"].ToString();
                    Phim.TenTrangThaiPhim = row["tenTrangThaiPhim"].ToString();
                    Phim.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Phim);
                }
                return ds;
            }
            return null;
        }

        public BindingList<PhimDTO> GetList_QL_DaXoa()
        {

            DataTable dt = new DataTable();
            BindingList<PhimDTO> ds = new BindingList<PhimDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Phim_QL_DaXoa", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    PhimDTO Phim = new PhimDTO();
                    Phim.GioiThieuPhim = row["gioiThieuBoPhim"].ToString();
                    Phim.HangSanXuat = row["hangSanXuat"].ToString();
                    Phim.MaDinhDangPhim = int.Parse(row["maDinhDangPhim"].ToString());
                    Phim.MaPhim = int.Parse(row["maPhim"].ToString());
                    Phim.MaTrangThaiPhim = int.Parse(row["maTrangThaiPhim"].ToString());
                    Phim.NamSanXuat = int.Parse(row["namSanXuat"].ToString());
                    Phim.Poster = row["poster"].ToString();
                    Phim.TenPhim = row["tenPhim"].ToString();
                    Phim.ThoiLuongPhim = int.Parse(row["thoiLuongPhim"].ToString());
                    Phim.Trailer = row["trailer"].ToString();
                    Phim.TenDinhDangPhim = row["tenLoaiKyThuat"].ToString();
                    Phim.TenTrangThaiPhim = row["tenTrangThaiPhim"].ToString();
                    Phim.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Phim);
                }
                return ds;
            }
            return null;
        }

        public BindingList<PhimDTO> GetListLikeName_QL(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<PhimDTO> ds = new BindingList<PhimDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_Phim_QL", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    PhimDTO Phim = new PhimDTO();
                    Phim.GioiThieuPhim = row["gioiThieuBoPhim"].ToString();
                    Phim.HangSanXuat = row["hangSanXuat"].ToString();
                    Phim.MaDinhDangPhim = int.Parse(row["maDinhDangPhim"].ToString());
                    Phim.MaPhim = int.Parse(row["maPhim"].ToString());
                    Phim.MaTrangThaiPhim = int.Parse(row["maTrangThaiPhim"].ToString());
                    Phim.NamSanXuat = int.Parse(row["namSanXuat"].ToString());
                    Phim.Poster = row["poster"].ToString();
                    Phim.TenPhim = row["tenPhim"].ToString();
                    Phim.ThoiLuongPhim = int.Parse(row["thoiLuongPhim"].ToString());
                    Phim.Trailer = row["trailer"].ToString();
                    Phim.TenDinhDangPhim = row["tenLoaiKyThuat"].ToString();
                    Phim.TenTrangThaiPhim = row["tenTrangThaiPhim"].ToString();
                    Phim.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Phim);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<PhimDTO> GetListLikeName_QL_DaXoa(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<PhimDTO> ds = new BindingList<PhimDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_Phim_QL_DaXoa", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    PhimDTO Phim = new PhimDTO();
                    Phim.GioiThieuPhim = row["gioiThieuBoPhim"].ToString();
                    Phim.HangSanXuat = row["hangSanXuat"].ToString();
                    Phim.MaDinhDangPhim = int.Parse(row["maDinhDangPhim"].ToString());
                    Phim.MaPhim = int.Parse(row["maPhim"].ToString());
                    Phim.MaTrangThaiPhim = int.Parse(row["maTrangThaiPhim"].ToString());
                    Phim.NamSanXuat = int.Parse(row["namSanXuat"].ToString());
                    Phim.Poster = row["poster"].ToString();
                    Phim.TenPhim = row["tenPhim"].ToString();
                    Phim.ThoiLuongPhim = int.Parse(row["thoiLuongPhim"].ToString());
                    Phim.Trailer = row["trailer"].ToString();
                    Phim.TenDinhDangPhim = row["tenLoaiKyThuat"].ToString();
                    Phim.TenTrangThaiPhim = row["tenTrangThaiPhim"].ToString();
                    Phim.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(Phim);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public PhimDTO GetFromId(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetFromID_Phim", sqlParameter);
                dp.CloseConnect();
                PhimDTO Phim = new PhimDTO();
                foreach (DataRow row in dt.Rows)
                {

                    Phim.MaPhim = int.Parse(row["maPhim"].ToString());
                    Phim.TenPhim = row["tenPhim"].ToString();
                    Phim.NamSanXuat = int.Parse(row["namSanXuat"].ToString());
                    Phim.HangSanXuat = row["hangSanXuat"].ToString();
                    Phim.GioiThieuPhim = row["gioiThieuBoPhim"].ToString();
                    Phim.Trailer = row["trailer"].ToString();
                    Phim.Poster = row["poster"].ToString();
                    Phim.ThoiLuongPhim = int.Parse(row["thoiLuongPhim"].ToString());
                    Phim.MaDinhDangPhim = int.Parse(row["maDinhDangPhim"].ToString());
                    Phim.MaTrangThaiPhim = int.Parse(row["maTrangThaiPhim"].ToString());
                    Phim.TenDinhDangPhim = row["tenLoaiKyThuat"].ToString();
                    Phim.TenTrangThaiPhim = row["TenTrangThaiPhim"].ToString();
                }
                return Phim;
            }
            return null;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_Phim", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_Phim", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(PhimDTO _P)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                     new SqlParameter("@gioiThieuBoPhim", _P.GioiThieuPhim),
                     new SqlParameter("@hangSanXuat", _P.HangSanXuat), 
                     new SqlParameter("@maDinhDangPhim", _P.MaDinhDangPhim),
                     new SqlParameter("@maTrangThaiPhim", _P.MaTrangThaiPhim),
                     new SqlParameter("@namSanXuat", _P.NamSanXuat), 
                     new SqlParameter("@poster", _P.Poster),
                     new SqlParameter("@tenPhim", _P.TenPhim), 
                     new SqlParameter("@thoiLuongPhim", _P.ThoiLuongPhim),
                     new SqlParameter("@trailer", _P.Trailer)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_Phim", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public bool Update(PhimDTO _P)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                     new SqlParameter("@maPhim", _P.MaPhim),
                     new SqlParameter("@gioiThieuBoPhim", _P.GioiThieuPhim),
                     new SqlParameter("@hangSanXuat", _P.HangSanXuat), 
                     new SqlParameter("@maDinhDangPhim", _P.MaDinhDangPhim),
                     new SqlParameter("@maTrangThaiPhim", _P.MaTrangThaiPhim),
                     new SqlParameter("@namSanXuat", _P.NamSanXuat), 
                     new SqlParameter("@poster", _P.Poster),
                     new SqlParameter("@tenPhim", _P.TenPhim), 
                     new SqlParameter("@thoiLuongPhim", _P.ThoiLuongPhim),
                     new SqlParameter("@trailer", _P.Trailer)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_Phim", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
    }
}
